import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_oldman_health/api/message_service.dart';
import 'package:flutter_oldman_health/models/family_add_model.dart';
import 'package:flutter_oldman_health/models/message_model.dart';
import 'package:flutter_oldman_health/models/response_data_model.dart';
import 'package:flutter_oldman_health/utils/remote_control_utils.dart';
import 'package:flutter_oldman_health/utils/utils.dart';

import 'event_bus.dart';

class FamilyAddAssist {
  /// 私有构造函数
  FamilyAddAssist._instance();

  List<String> relationIdList = [];

  static final FamilyAddAssist _singleton = FamilyAddAssist._instance();

  ///工厂构造函数
  factory FamilyAddAssist() => _singleton;

  void setEventBus(BuildContext context) {

    EventBus().on("FamilyAdd", (event) {
      FamilyAddModel familyAddModel =
          FamilyAddModel.fromJson(json.decode(event));
      print(familyAddModel);
      String name = familyAddModel.content.memberName;
      String id = familyAddModel.content.memberId;
      print(name);

      showRemindDialog(context, name, id);
    });
  }

  void offEventBus() {
    print("off FamilyAdd");
    //EventBus().off("FamilyAdd");
  }

  Future showRemindDialog(
      BuildContext context, String memberName, String memberId) async {
    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
              title: Text("新成员添加提醒"),
              content: Text("$memberName申请添加您为家庭成员，是否同意？"),
              actions: <Widget>[
                ElevatedButton(
                    onPressed: () {
                      setMemberMessageHasRead(memberId);
                      Navigator.pop(context);
                    },
                    child:
                        Padding(padding: EdgeInsets.all(8), child: Text("拒绝")),
                    style: ButtonStyle(
                        textStyle:
                            MaterialStateProperty.all(TextStyle(fontSize: 17)),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          //设置 界面效果 圆角弧度
                          style: BorderStyle.solid,
                                color: Colors.grey[400]
                        ))))),
                ElevatedButton(
                    onPressed: () async {
                      MessageService.confirmFamilyAdd(memberId).then((value) {
                        if (null != value && value.statusCode == 200) {
                          ResponseDataModel model =
                              ResponseDataModel.fromJson(value.data);
                          if (model.success()) {
                            RemoteControlTool().replyFamilyAddRequest(memberId);
                            setMemberMessageHasRead(memberId);
                            Navigator.pop(context);
                          }
                        }
                      });
                    },
                    child:
                        Padding(padding: EdgeInsets.all(8), child: Text("同意")),
                    style: ButtonStyle(
                        textStyle:
                            MaterialStateProperty.all(TextStyle(fontSize: 17)),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          //设置 界面效果 圆角弧度
                          style: BorderStyle.solid,
                          color: Color(0xffFFffff),
                        ))))),
              ]);
        });
    return result;
  }

  void setMemberMessageHasRead(String memberId) {
    MessageService.queryMemberMessageUnRead(memberId).then((value) {
      if (value != null && value.statusCode == 200) {
        MessageAddModel response = MessageAddModel.fromJson(value.data);
        if (response != null && response.success() && response.data != null) {
          List<MessageAddItem> msgList = response.data;
          List<String> msgUnReadIdList = [];
          if (relationIdList.isNotEmpty) {
            for (int i = 0; i < msgList.length; i++) {
              if (relationIdList.contains(msgList[i].senderId)) {
                msgUnReadIdList.add(msgList[i].senderId);
              }
            }
            if (relationIdList.isNotEmpty) {
              MessageService.setMemberMessageHasRead(relationIdList);
            }
          }
          // EasyLoading.showSuccess(response.msg);
        } else {
          EasyLoading.showInfo(response.msg);
        }
      }
    });
  }
}
